package com.ieasy.module.oa.project.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ieasy.basic.dao.IBaseDao;
import com.ieasy.basic.model.DataGrid;
import com.ieasy.basic.model.Msg;
import com.ieasy.basic.util.BeanUtils;
import com.ieasy.basic.util.NumberUtil;
import com.ieasy.basic.util.date.DateUtils;
import com.ieasy.basic.util.dbutil.IDBUtilsHelper;
import com.ieasy.module.common.service.BaseService;
import com.ieasy.module.oa.project.entity.PosistionQuotientReportEntity;
import com.ieasy.module.oa.project.entity.ProjectCenter;
import com.ieasy.module.oa.project.web.form.CalQuotientForm;
import com.ieasy.module.oa.project.web.form.CompanyProjectQuotientForm;
import com.ieasy.module.oa.project.web.form.PosistionQuotientReportForm;
import com.ieasy.module.oa.project.web.form.ProjectCenterForm;
import com.ieasy.module.oa.project.web.form.ProjectDevWorkTimeForm;
import com.ieasy.module.oa.project.web.form.ProjectRyListForm;
import com.ieasy.module.system.web.form.QuotientForm;

@Service @Transactional
public class PositionQuotienService extends BaseService implements IPositionQuotienService {
	
	private Log LOG = LogFactory.getLog(PositionQuotienService.class) ;
	
	@Inject
	private IDBUtilsHelper dbutil;
	
	@Inject
	private IBaseDao<PosistionQuotientReportEntity> basedaoPositionQuotient;
	
	@Inject
	private IBaseDao<ProjectCenter> basedaoProject;
	
	@Inject
	private IProjectDevWorkTimeService projectDevWorkTimeService ;
	
	@Inject
	private ICompanyProjectQuotientService companyProjectQuotientService ;
	
	@Autowired
	private IProjectRyListService ryListService ;
	
	@Autowired
	private IProjectJdlService2 jdlService2 ;
	
	@SuppressWarnings("unchecked")
	@Override
	public DataGrid datagrid(PosistionQuotientReportForm form) {
		//根据ProjectID和year查询“ieasy_oa_position_quotient”表中是否有记录存在
		//如果不存在则将该项目的所有的年份的数据导入到数据库
		insertAllYeayData(form);
		
		List<PosistionQuotientReportForm> list = new ArrayList<PosistionQuotientReportForm>() ;
		
		List<Map<String, Object>> footerList = new ArrayList<Map<String, Object>>() ;
		Map<String, Object> map = new HashMap<String, Object>() ;
		map.put("m1", 0);
		map.put("m2", 0);
		map.put("m3", 0);
		map.put("m4", 0);
		map.put("m5", 0);
		map.put("m6", 0);
		map.put("m7", 0);
		map.put("m8", 0);
		map.put("m9", 0);
		map.put("m10", 0);
		map.put("m11", 0);
		map.put("m12", 0);
		
		map.put("q1", 0);
		map.put("q2", 0);
		map.put("q3", 0);
		map.put("q4", 0);
		map.put("q5", 0);
		map.put("q6", 0);
		map.put("q7", 0);
		map.put("q8", 0);
		map.put("q9", 0);
		map.put("q10", 0);
		map.put("q11", 0);
		map.put("q12", 0);
		
		map.put("result1", 0);
		map.put("result2", 0);
		map.put("result3", 0);
		map.put("result4", 0);
		map.put("result5", 0);
		map.put("result6", 0);
		map.put("result7", 0);
		map.put("result8", 0);
		map.put("result9", 0);
		map.put("result10", 0);
		map.put("result11", 0);
		map.put("result12", 0);
		
		//1.根据ProjectID和年份获取岗位
		ProjectDevWorkTimeForm pdwf = new ProjectDevWorkTimeForm() ;
		pdwf.setProj_id(form.getProjectId());
		pdwf.setYear(form.getYear());
		DataGrid by_project_dev_position_datagrid = projectDevWorkTimeService.by_project_dev_position_datagrid(pdwf) ;
		List<ProjectDevWorkTimeForm> rows = (List<ProjectDevWorkTimeForm>) by_project_dev_position_datagrid.getRows() ;
		
		//2.根据ProjectID和year查询“ieasy_oa_position_quotient”表中是否有记录存在
		List<PosistionQuotientReportForm> quotients = this.basedaoPositionQuotient.listSQL("select year from ieasy_oa_position_quotient where projectId=? and year=?", new Object[]{form.getProjectId(), form.getYear()}, PosistionQuotientReportForm.class, false) ;
		
		//3.记录存在，获取“ieasy_oa_position_quotient”表中记录，如果获取的岗位与现有记录的个数不一直，则说明获取的岗位中新增岗位
		//在现有岗位中追加新岗位
		if(null != quotients && quotients.size() > 0) {
			Map<String, Object> mapHb = new HashMap<String, Object>() ;
			for (ProjectDevWorkTimeForm projectDevWorkTimeForm : rows) {
				//将新的岗位加入到Map中，待和数据库中的对比，如果相同则删除Map中的数据，剩余的将加入到数据库中
				mapHb.put(projectDevWorkTimeForm.getPositionId(), projectDevWorkTimeForm) ;
			}
			
			//直接从“ieasy_oa_position_quotient”表中取出记录显示，岗位人月、岗位系数、结果，都是计算好的
			List<PosistionQuotientReportForm> listQR = this.find(form) ;
			for (PosistionQuotientReportForm qr : listQR) {
				list.add(qr) ;
				
				//根据岗位ID删除Map中的数据（岗位）
				mapHb.remove(qr.getPositionId());
			}
			
			//如果map中还有数据，说明有新的岗位，则将数据添加到数据库
			if(!mapHb.isEmpty()) {
				for (String key : mapHb.keySet()) {
					ProjectDevWorkTimeForm pd = (ProjectDevWorkTimeForm) mapHb.get(key) ;
					list.add(this.insertQuotientPositionReportRecord(pd, form)) ;
				}
			}
			
		} else {//4.记录不存在，根据获取的岗位创建记录
			for (ProjectDevWorkTimeForm pd : rows) {
				//保存记录，并添加到list中，呈现客户端
				list.add(this.insertQuotientPositionReportRecord(pd, form)) ;
			}
		}
		
		
		//页脚数据
		for (PosistionQuotientReportForm pq : list) {
			//岗位人月合计
			map.put("m1", (null!= map.get("m1")? NumberUtil.formatNum(Float.parseFloat(map.get("m1").toString()) + pq.getM1()) : pq.getM1())) ;
			map.put("m2", (null!= map.get("m2")? NumberUtil.formatNum(Float.parseFloat(map.get("m2").toString()) + pq.getM2()) : pq.getM2())) ;
			map.put("m3", (null!= map.get("m3")? NumberUtil.formatNum(Float.parseFloat(map.get("m3").toString()) + pq.getM3()) : pq.getM3())) ;
			map.put("m4", (null!= map.get("m4")? NumberUtil.formatNum(Float.parseFloat(map.get("m4").toString()) + pq.getM4()) : pq.getM4())) ;
			map.put("m5", (null!= map.get("m5")? NumberUtil.formatNum(Float.parseFloat(map.get("m5").toString()) + pq.getM5()) : pq.getM5())) ;
			map.put("m6", (null!= map.get("m6")? NumberUtil.formatNum(Float.parseFloat(map.get("m6").toString()) + pq.getM6()) : pq.getM6())) ;
			map.put("m7", (null!= map.get("m7")? NumberUtil.formatNum(Float.parseFloat(map.get("m7").toString()) + pq.getM7()) : pq.getM7())) ;
			map.put("m8", (null!= map.get("m8")? NumberUtil.formatNum(Float.parseFloat(map.get("m8").toString()) + pq.getM8()) : pq.getM8())) ;
			map.put("m9", (null!= map.get("m9")? NumberUtil.formatNum(Float.parseFloat(map.get("m9").toString()) + pq.getM9()) : pq.getM9())) ;
			map.put("m10", (null!= map.get("m10")? NumberUtil.formatNum(Float.parseFloat(map.get("m10").toString()) + pq.getM10()) : pq.getM10())) ;
			map.put("m11", (null!= map.get("m11")? NumberUtil.formatNum(Float.parseFloat(map.get("m11").toString()) + pq.getM11()) : pq.getM11())) ;
			map.put("m12", (null!= map.get("m12")? NumberUtil.formatNum(Float.parseFloat(map.get("m12").toString()) + pq.getM12()) : pq.getM12())) ;
			
			//结果合计
			map.put("result1", (null!= map.get("result1")? NumberUtil.formatNum(Float.parseFloat(map.get("result1").toString()) + pq.getResult1()) : pq.getResult1())) ;
			map.put("result2", (null!= map.get("result2")? NumberUtil.formatNum(Float.parseFloat(map.get("result2").toString()) + pq.getResult2()) : pq.getResult2())) ;
			map.put("result3", (null!= map.get("result3")? NumberUtil.formatNum(Float.parseFloat(map.get("result3").toString()) + pq.getResult3()) : pq.getResult3())) ;
			map.put("result4", (null!= map.get("result4")? NumberUtil.formatNum(Float.parseFloat(map.get("result4").toString()) + pq.getResult4()) : pq.getResult4())) ;
			map.put("result5", (null!= map.get("result5")? NumberUtil.formatNum(Float.parseFloat(map.get("result5").toString()) + pq.getResult5()) : pq.getResult5())) ;
			map.put("result6", (null!= map.get("result6")? NumberUtil.formatNum(Float.parseFloat(map.get("result6").toString()) + pq.getResult6()) : pq.getResult6())) ;
			map.put("result7", (null!= map.get("result7")? NumberUtil.formatNum(Float.parseFloat(map.get("result7").toString()) + pq.getResult7()) : pq.getResult7())) ;
			map.put("result8", (null!= map.get("result8")? NumberUtil.formatNum(Float.parseFloat(map.get("result8").toString()) + pq.getResult8()) : pq.getResult8())) ;
			map.put("result9", (null!= map.get("result9")? NumberUtil.formatNum(Float.parseFloat(map.get("result9").toString()) + pq.getResult9()) : pq.getResult9())) ;
			map.put("result10", (null!= map.get("result10")? NumberUtil.formatNum(Float.parseFloat(map.get("result10").toString()) + pq.getResult10()) : pq.getResult10())) ;
			map.put("result11", (null!= map.get("result11")? NumberUtil.formatNum(Float.parseFloat(map.get("result11").toString()) + pq.getResult11()) : pq.getResult11())) ;
			map.put("result12", (null!= map.get("result12")? NumberUtil.formatNum(Float.parseFloat(map.get("result12").toString()) + pq.getResult12()) : pq.getResult12())) ;
			
		}
		
		DataGrid dg = new DataGrid();
		dg.setRows(list);
		footerList.add(map) ;
		dg.setFooter(footerList) ;
		
		return dg;
	}
	
	/**
	 * 根据ProjectID和year查询“ieasy_oa_position_quotient”表中是否有记录存在
	 * 如果不存在则将该项目的所有的年份的数据导入到数据库
	 * @param form
	 */
	private void insertAllYeayData(PosistionQuotientReportForm form) {
		
		//根据ProjectID和year查询“ieasy_oa_position_quotient”表中是否有记录存在
		List<PosistionQuotientReportForm> quotients = this.basedaoPositionQuotient.listSQL("select year from ieasy_oa_position_quotient where projectId=?", new Object[]{form.getProjectId()}, PosistionQuotientReportForm.class, false) ;
		
		if(null == quotients || quotients.isEmpty()) {
			ProjectCenterForm project = (ProjectCenterForm) this.basedaoProject.queryObjectSQL("select t.proj_start_time, t.proj_end_time from ieasy_oa_project_center t where t.id=?", new Object[]{form.getProjectId()}, ProjectCenterForm.class, false) ;
			Integer startYear = DateUtils.getYear(project.getProj_start_time()) ;
			Integer endYear = DateUtils.getYear(project.getProj_end_time()) ;
			
			for(int i=startYear; i<=endYear; i++) {
				ProjectDevWorkTimeForm pdwf = new ProjectDevWorkTimeForm() ;
				pdwf.setProj_id(form.getProjectId());
				pdwf.setYear(i);
				DataGrid by_project_dev_position_datagrid = projectDevWorkTimeService.by_project_dev_position_datagrid(pdwf) ;
				
				form.setYear(i);
				@SuppressWarnings("unchecked")
				List<ProjectDevWorkTimeForm> rows = (List<ProjectDevWorkTimeForm>) by_project_dev_position_datagrid.getRows() ;
				for (ProjectDevWorkTimeForm pd : rows) {
					this.insertQuotientPositionReportRecord(pd, form);
					basedaoPositionQuotient.getCurrentSession().flush();
				}
			}
			
			
		}
	}
	
	//数据整理并保存
	private PosistionQuotientReportForm insertQuotientPositionReportRecord(ProjectDevWorkTimeForm pd, PosistionQuotientReportForm form) {
		PosistionQuotientReportForm q = new PosistionQuotientReportForm() ;
		q.setProjectId(form.getProjectId());
		q.setYear(form.getYear());
		q.setPositionId(pd.getPositionId());
		q.setPositionName(pd.getPositionName());
		
		//设置岗位人月
		q.setM1(pd.getM1());
		q.setM2(pd.getM2());
		q.setM3(pd.getM3());
		q.setM4(pd.getM4());
		q.setM5(pd.getM5());
		q.setM6(pd.getM6());
		q.setM7(pd.getM7());
		q.setM8(pd.getM8());
		q.setM9(pd.getM9());
		q.setM10(pd.getM10());
		q.setM11(pd.getM11());
		q.setM12(pd.getM12());
		
		
		//取得岗位当前选择年份默认的系数
		QuotientForm quotientByPosition = getQuotientBypositionId(pd.getPositionId(), form.getYear()) ;
		//设置默认默认岗位系数
		q.setQ1(null != quotientByPosition ? quotientByPosition.getQuotient() : 0);
		q.setQ2(null != quotientByPosition ? quotientByPosition.getQuotient() : 0);
		q.setQ3(null != quotientByPosition ? quotientByPosition.getQuotient() : 0);
		q.setQ4(null != quotientByPosition ? quotientByPosition.getQuotient() : 0);
		q.setQ5(null != quotientByPosition ? quotientByPosition.getQuotient() : 0);
		q.setQ6(null != quotientByPosition ? quotientByPosition.getQuotient() : 0);
		q.setQ7(null != quotientByPosition ? quotientByPosition.getQuotient() : 0);
		q.setQ8(null != quotientByPosition ? quotientByPosition.getQuotient() : 0);
		q.setQ9(null != quotientByPosition ? quotientByPosition.getQuotient() : 0);
		q.setQ10(null != quotientByPosition ? quotientByPosition.getQuotient() : 0);
		q.setQ11(null != quotientByPosition ? quotientByPosition.getQuotient() : 0);
		q.setQ12(null != quotientByPosition ? quotientByPosition.getQuotient() : 0);
		
		//计算结果 (岗位人月*岗位系数)
		q.setResult1(NumberUtil.formatNum(q.getM1() * q.getQ1()));
		q.setResult2(NumberUtil.formatNum(q.getM2() * q.getQ2()));
		q.setResult3(NumberUtil.formatNum(q.getM3() * q.getQ3()));
		q.setResult4(NumberUtil.formatNum(q.getM4() * q.getQ4()));
		q.setResult5(NumberUtil.formatNum(q.getM5() * q.getQ5()));
		q.setResult6(NumberUtil.formatNum(q.getM6() * q.getQ6()));
		q.setResult7(NumberUtil.formatNum(q.getM7() * q.getQ7()));
		q.setResult8(NumberUtil.formatNum(q.getM8() * q.getQ8()));
		q.setResult9(NumberUtil.formatNum(q.getM9() * q.getQ9()));
		q.setResult10(NumberUtil.formatNum(q.getM10() * q.getQ10()));
		q.setResult11(NumberUtil.formatNum(q.getM11() * q.getQ11()));
		q.setResult12(NumberUtil.formatNum(q.getM12() * q.getQ12()));
		
		insertPositionQuotient(q) ;
		
		return q ;
	}
	
	private QuotientForm getQuotientBypositionId(String positionId, Integer year) {
		return (QuotientForm) this.basedaoPositionQuotient.queryObjectSQL("select year, quotient from ieasy_sys_position_quotient where position_id=? and year=?", new Object[]{positionId, year}, QuotientForm.class, false) ;
	}
	
	@Override
	public void insertPositionQuotient(PosistionQuotientReportForm form) {
		PosistionQuotientReportEntity e = new PosistionQuotientReportEntity() ;
		BeanUtils.copyNotNullProperties(form, e);
		this.basedaoPositionQuotient.add(e) ;
		
		//把保存的ID放回form中，否则在执行完删除原数据后在进行修改，则找不到ID
		form.setId(e.getId());
	}
	
	@Override
	public void updatePositionQuotient(PosistionQuotientReportForm form) {
		PosistionQuotientReportEntity e = new PosistionQuotientReportEntity() ;
		BeanUtils.copyNotNullProperties(form, e);
		
		//计算结果 (岗位人月*岗位系数)
		e.setResult1(NumberUtil.formatNum(e.getM1() * e.getQ1()));
		e.setResult2(NumberUtil.formatNum(e.getM2() * e.getQ2()));
		e.setResult3(NumberUtil.formatNum(e.getM3() * e.getQ3()));
		e.setResult4(NumberUtil.formatNum(e.getM4() * e.getQ4()));
		e.setResult5(NumberUtil.formatNum(e.getM5() * e.getQ5()));
		e.setResult6(NumberUtil.formatNum(e.getM6() * e.getQ6()));
		e.setResult7(NumberUtil.formatNum(e.getM7() * e.getQ7()));
		e.setResult8(NumberUtil.formatNum(e.getM8() * e.getQ8()));
		e.setResult9(NumberUtil.formatNum(e.getM9() * e.getQ9()));
		e.setResult10(NumberUtil.formatNum(e.getM10() * e.getQ10()));
		e.setResult11(NumberUtil.formatNum(e.getM11() * e.getQ11()));
		e.setResult12(NumberUtil.formatNum(e.getM12() * e.getQ12()));
		
		this.basedaoPositionQuotient.update(e);
	}
	
	
	private List<PosistionQuotientReportForm> find(PosistionQuotientReportForm form) {
		Map<String, Object> alias = new HashMap<String, Object>();
		String sql = "select " +
					 "* "+
					 "from ieasy_oa_position_quotient t " +
				 	 "where 1=1 ";
		
		sql = addWhere(sql, form, alias) ;
		return this.basedaoPositionQuotient.listSQL(sql, alias, PosistionQuotientReportForm.class, false) ;
	}
	
	private String addWhere(String sql, PosistionQuotientReportForm form, Map<String, Object> params) {
		if (null != form.getYear()) {
			sql += " and t.year =:year";
			params.put("year", form.getYear());
		}
		if (null != form.getProjectId()) {
			sql += " and t.projectId =:projectId";
			params.put("projectId", form.getProjectId());
		}
		return sql ;
	}

	@Override
	public Msg deleteByProjectYear(PosistionQuotientReportForm form) {
		try {
			//删除开发人员的数据
			this.dbutil.getQr().update("delete from ieasy_oa_position_quotient where projectId=? and year=?", new Object[]{
					form.getProjectId(), form.getYear()
			}) ;
			return new Msg(true, "删除成功！") ;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return new Msg(false, "删除失败！") ;
	}

	@Override
	public DataGrid calByQuotient(PosistionQuotientReportForm form) {
		List<CalQuotientForm> list = new ArrayList<CalQuotientForm>() ;
		
		//取得实际总人月数
		Float sjrys = getSJSZRYS(form.getProjectId());
		LOG.debug("实际总人月数：" + sjrys + "==="+ "开始年份：" + form.getStartYear() + "==结束年份：" + form.getEndYear()+"\r\n"+"=====================================================================================");
		
		//取得岗位人月的总人月数
		Map<String, Object> map = new LinkedHashMap<String, Object>() ;
		
		//计算实际消耗人月数
		//公式：(实际总人月数 / 公式项目系数) - 1~12月岗位总人月数
		
		//*********1.非跨年项目*********
		if(form.getStartYear().equals(form.getEndYear())) {
			//根据查询年份取得该年的公司项目系数
			CompanyProjectQuotientForm companyQ = this.companyProjectQuotientService.getByYeay(form.getYear()) ;
			
			if(null != companyQ) {
				//计算剩余人月数
				calSingle(sjrys, companyQ.getQuotient(), list, map, form);
			}
			
		} else { //*********2.跨年项目*********
			//跨年项目（2013~2016） 计算2015年的人月投入
			//循环取得2013~2014的人月投入计算结果，将结果除以2015年的公司项目系数，作为2015年的“预定最大投入人月数”
			
			//1.如果查询的年份和项目的开始年份相等则按非跨年项目来计算
			if(form.getYear().equals(form.getStartYear())) {
				//根据查询年份取得该年的公司项目系数
				CompanyProjectQuotientForm companyQ = this.companyProjectQuotientService.getByYeay(form.getYear()) ;
				if(null != companyQ) {
					//计算剩余人月数
					calSingle(sjrys, companyQ.getQuotient(), list, map, form);
				}
			} else {//查询的年份和项目开始年份不相等
				//历史年份数据
				List<CalQuotientForm> historyList = new ArrayList<CalQuotientForm>() ;
				
				//上一年公司项目系数
				float preQuotient = 0 ;
				
				for(int i=form.getStartYear(); i <= form.getYear()-1; i++) {
					LOG.debug("循环的年份：" + i);
					//根据查询年份取得该年的公司项目系数
					CompanyProjectQuotientForm companyQ = this.companyProjectQuotientService.getByYeay(i) ;
					
					if(null != companyQ) {
						//第一次循环list为空，取得项目最开始实际总人月数“sjrys”，sjrys变量将在第二次循环开始变动，将12份的剩余人月数赋值给"sjrys"变量
						if(i==form.getStartYear()) {
							PosistionQuotientReportForm nf = new PosistionQuotientReportForm() ;
							nf.setProjectId(form.getProjectId());
							nf.setYear(i);
							
							preQuotient = companyQ.getQuotient() ;
							//计算剩余人月数
							calSingle(sjrys, companyQ.getQuotient(), historyList, map, nf);
							/*for (CalQuotientForm calQuotientForm : historyList) {
								LOG.debug("=="+calQuotientForm);
							}*/
							LOG.debug("====================第一年===============================");
						} else {
							//取得查询年份的上一年的最后的剩余人月数，作为“分子”
							CalQuotientForm c = historyList.get(historyList.size()-1) ;
							sjrys = c.getResult() ;
							historyList = new ArrayList<CalQuotientForm>() ;
							float currentQuotient = companyQ.getQuotient() ;
							//判断当前查询年份的公司项目系数是否大于当前查询年份的上年公司项目系数，如果大于则使用当前查询年份的公司项目系数作为“分母”
							LOG.debug(currentQuotient+"=="+preQuotient+"=="+(currentQuotient <= preQuotient));
							if(currentQuotient <= preQuotient) {
								currentQuotient = 1 ;
							} else {
								//正数时：取2016年12的剩余人月数 / （ 2017公司项目系数 / 2016公司项目系数）
								if(sjrys >= 0) {
									//计算上调率（当前查询年份的公司项目系数 / 上年的公司项目系数）
									LOG.debug("1预定最大投入人月：" + ( sjrys / (currentQuotient / preQuotient)) + "=" + "("+sjrys +"/("+ currentQuotient +"/"+ preQuotient +"))");
									sjrys = sjrys / (currentQuotient / preQuotient);
									
									currentQuotient = 1 ;
									
									//currentQuotient = currentQuotient / preQuotient ;
								} else { //负数时：取2016年12的剩余人月数 * （ 2017公司项目系数 / 2016公司项目系数）
									//计算上调率（当前查询年份的公司项目系数 / 上年的公司项目系数）
									LOG.debug("2预定最大投入人月：" + (sjrys * (currentQuotient / preQuotient)) + "=" + "("+sjrys +"*("+ currentQuotient +"/"+ preQuotient +"))");
									sjrys = sjrys * (currentQuotient / preQuotient);
									currentQuotient = 1 ;
									LOG.debug(sjrys);
									//currentQuotient = sjrys * (currentQuotient / preQuotient) ;
								}
							}
							
							PosistionQuotientReportForm nf = new PosistionQuotientReportForm() ;
							nf.setProjectId(form.getProjectId());
							nf.setYear(i);
							//计算剩余人月数
							calSingle(sjrys, currentQuotient, historyList, map, nf);
							/*for (CalQuotientForm calQuotientForm : historyList) {
								LOG.debug("=="+calQuotientForm);
							}*/
							LOG.debug("=======================================================");
						}
					}
				}

				if(!historyList.isEmpty()) {
					//取得查询年份的上一年的最后的剩余人月数
					CalQuotientForm c = historyList.get(historyList.size()-1) ;
					sjrys = c.getResult() ;
					
					//根据查询年份取得该年的公司项目系数
					CompanyProjectQuotientForm companyProjectQuotientForm = this.companyProjectQuotientService.getByYeay(form.getYear()) ;
					if(null != companyProjectQuotientForm) {
						float currentQuotient = companyProjectQuotientForm.getQuotient() ;
						
						//判断当前查询年份的公司项目系数是否大于当前查询年份的上年公司项目系数，如果大于则使用当前查询年份的公司项目系数作为分母
						if(currentQuotient <= preQuotient) {
							currentQuotient = 1;
						} else {
							if(sjrys >= 0) {
								//计算上调率（当前查询年份的公司项目系数 / 上年的公司项目系数）
								LOG.debug("3预定最大投入人月：" + ( sjrys / (currentQuotient / preQuotient)) + "=" + "("+sjrys +"/("+ currentQuotient +"/"+ preQuotient +"))");
								sjrys = sjrys / (currentQuotient / preQuotient);
								currentQuotient = 1 ;
								//currentQuotient = currentQuotient / preQuotient ;
							} else {
								LOG.debug("4预定最大投入人月：" + (sjrys * (currentQuotient / preQuotient)) + "=" + "("+sjrys +"*("+ currentQuotient +"/"+ preQuotient +"))");
								sjrys = sjrys * (currentQuotient / preQuotient);
								currentQuotient = 1 ;
								//currentQuotient = sjrys * (currentQuotient / preQuotient) ;
							}
						}
						
						
						//开始计算查询年份的结果，输入的实际人月数为上一年最后月份的剩余人月数
						calSingle(sjrys, currentQuotient, list, map, form);
					}
				}
				
			}
		}
		
		DataGrid dg = new DataGrid();
		dg.setRows(list);
		return dg;
	}
	
	/**
	 * 计算剩余人月数
	 * @param sjrys
	 * @param quotient
	 * @param list
	 * @param map
	 */
	private void calSingle(float sjrys, float quotient, List<CalQuotientForm> list, Map<String, Object> map, PosistionQuotientReportForm form) {
		map = new LinkedHashMap<String, Object>() ;
		List<PosistionQuotientReportForm> listQR = this.find(form) ;
		for (PosistionQuotientReportForm pq : listQR) {
			//结果合计
			map.put("result1", (null!= map.get("result1")? NumberUtil.formatNum(Float.parseFloat(map.get("result1").toString()) + pq.getResult1()) : pq.getResult1())) ;
			map.put("result2", (null!= map.get("result2")? NumberUtil.formatNum(Float.parseFloat(map.get("result2").toString()) + pq.getResult2()) : pq.getResult2())) ;
			map.put("result3", (null!= map.get("result3")? NumberUtil.formatNum(Float.parseFloat(map.get("result3").toString()) + pq.getResult3()) : pq.getResult3())) ;
			map.put("result4", (null!= map.get("result4")? NumberUtil.formatNum(Float.parseFloat(map.get("result4").toString()) + pq.getResult4()) : pq.getResult4())) ;
			map.put("result5", (null!= map.get("result5")? NumberUtil.formatNum(Float.parseFloat(map.get("result5").toString()) + pq.getResult5()) : pq.getResult5())) ;
			map.put("result6", (null!= map.get("result6")? NumberUtil.formatNum(Float.parseFloat(map.get("result6").toString()) + pq.getResult6()) : pq.getResult6())) ;
			map.put("result7", (null!= map.get("result7")? NumberUtil.formatNum(Float.parseFloat(map.get("result7").toString()) + pq.getResult7()) : pq.getResult7())) ;
			map.put("result8", (null!= map.get("result8")? NumberUtil.formatNum(Float.parseFloat(map.get("result8").toString()) + pq.getResult8()) : pq.getResult8())) ;
			map.put("result9", (null!= map.get("result9")? NumberUtil.formatNum(Float.parseFloat(map.get("result9").toString()) + pq.getResult9()) : pq.getResult9())) ;
			map.put("result10", (null!= map.get("result10")? NumberUtil.formatNum(Float.parseFloat(map.get("result10").toString()) + pq.getResult10()) : pq.getResult10())) ;
			map.put("result11", (null!= map.get("result11")? NumberUtil.formatNum(Float.parseFloat(map.get("result11").toString()) + pq.getResult11()) : pq.getResult11())) ;
			map.put("result12", (null!= map.get("result12")? NumberUtil.formatNum(Float.parseFloat(map.get("result12").toString()) + pq.getResult12()) : pq.getResult12())) ;
			
		}
		
		
		int i=1;
		float temp = 0 ;
		float a = sjrys / quotient ;
		LOG.debug("[5预定最大投入人月  / 公式项目系数]"+sjrys + "/" + quotient +"="+ a);
		float c = 0 ;
		for(String key : map.keySet()) {
			float b = NumberUtil.formatNum(Float.parseFloat(map.get(key).toString())) ;
			if(i==1) {
				c = a - b ;
				list.add(new CalQuotientForm(i+"月", NumberUtil.formatNum(a), b, NumberUtil.formatNum(c))) ;
			} else {
				temp = c;
				c = c - b ;
				list.add(new CalQuotientForm(i+"月", NumberUtil.formatNum(temp), b, NumberUtil.formatNum(c))) ;
			}
			i++;
		}
	}
	
	/**
	 * 取得项目实际总人月数
	 * @param projectId
	 * @return
	 */
	private Float getSJSZRYS(String projectId) {
		Map<String, Object> alias = new HashMap<String, Object>();
		String sql = "select " + 
				"p.id, p.proj_bjzry, p.proj_name from ieasy_oa_project_center p " + 
				"where 1=1 and id='"+projectId+"'";
		
		ProjectCenterForm projectForm = (ProjectCenterForm) this.basedaoProject.queryObjectSQL(sql, alias, ProjectCenterForm.class, false);
		
		//计算实际受注人月数
		projectForm.setSjszrns(null != projectForm.getProj_bjzry() ? Float.parseFloat(projectForm.getProj_bjzry()) : 0);
		if(projectForm.getProj_bjzry() != null) {
			//取得变更的人月数
			ProjectRyListForm ryForm = new ProjectRyListForm() ;
			ryForm.setProjectId(projectId);
			List<ProjectRyListForm> list = this.ryListService.list(ryForm) ;
			for (ProjectRyListForm projectRyListForm : list) {
				if(projectRyListForm.getType().equals("+")) {
					projectForm.setSjszrns(projectForm.getSjszrns() + projectRyListForm.getRy()) ;
				} else {
					projectForm.setSjszrns(projectForm.getSjszrns() - projectRyListForm.getRy()) ;
				}
			}
		}
		return projectForm.getSjszrns() ;
	}
	
}
